package org.jeecg.modules.business.third.strategy.sap;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.delivery.entity.WmsDeliveryTaskMaterial;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.dock.entity.dto.request.MaterialTransferOrderRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.SapBaseResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r105Delivery")
@Slf4j
@RequiredArgsConstructor
public class R105Delivery implements SapOperateStrategy {

    @Value("${transtella.bus.factory}")
    private String factory;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    @Override
    public List operate(Map<String, Object> map) {
        log.info("四期到一二期调拨任务开始");
        List<MaterialTransferOrderRequestDTO> list = new ArrayList<>();
        List<WmsDeliveryTaskMaterial> deliveryTaskMaterials = (List<WmsDeliveryTaskMaterial>) map.get("deliveryData");
        if (CollectionUtil.isEmpty(deliveryTaskMaterials)) {
            List<SapBaseResponseDTO> sapBaseResponseDTOS = new ArrayList<>();
            SapBaseResponseDTO sapBaseResponseDTO = new SapBaseResponseDTO();
            sapBaseResponseDTO.setResult("E");
            sapBaseResponseDTO.setMessage("WMS无调拨任务需要过账");
            sapBaseResponseDTOS.add(sapBaseResponseDTO);
            return sapBaseResponseDTOS;
        }
        if (ObjectUtil.isNotNull(map.get("data"))) {
            Object data = map.get("data");
            String jsonString = JSON.toJSONString(data);
            List<WmsInReceiptMaterial> wmsInReceiptMaterials = JSON.parseArray(jsonString,WmsInReceiptMaterial.class);
            List<String> collect = wmsInReceiptMaterials.stream().map(WmsInReceiptMaterial::getBatchCode).collect(Collectors.toList());
            deliveryTaskMaterials = deliveryTaskMaterials.stream().filter(s -> collect.contains(s.getBatchCode())).collect(Collectors.toList());
        }

        String uuid = IdUtil.fastSimpleUUID();
        for (WmsDeliveryTaskMaterial deliveryTaskMaterial : deliveryTaskMaterials) {
            MaterialTransferOrderRequestDTO requestDTO = new MaterialTransferOrderRequestDTO();
            requestDTO.setNumber(deliveryTaskMaterial.getDeliveryQuantity());
            requestDTO.setUuid(uuid);
            String documentType = (String) map.get("documentType");
            if (documentType.equals("R101")) {
                requestDTO.setCompanyCode("10");
            } else if (documentType.equals("R105")) {
                requestDTO.setCompanyCode("20");
            } else {
                throw new JeecgBootException("当前单据类型未匹配，请联系相关工作人员");
            }

            requestDTO.setMaterialCode(deliveryTaskMaterial.getMotherMaterialCode());

            requestDTO.setBatchCode(deliveryTaskMaterial.getMotherBatchCode());
            requestDTO.setWeBatchCode(deliveryTaskMaterial.getBatchCode());
//                String outWarehouseId = taskMaterials.get(0).getOutWarehouseId();
//                List<WmsSapWarehouseMap> outWarehouseList = wmsSapWarehouseMapService.list(new LambdaQueryWrapper<WmsSapWarehouseMap>().eq(WmsSapWarehouseMap::getWmsBasicWarehouseId, outWarehouseId));
//                if (CollectionUtil.isEmpty(outWarehouseList)) {
//                    throw new JeecgBootException("当前出库库位在wms系统中未找到对应的sap库位");
//                }
//                requestDTO.setSetOutFactory(outWarehouseList.get(0).getSapLocation());
//                requestDTO.setFactory(outWarehouseList.get(0).getFactory());

//                String inWarehouseId = taskMaterials.get(0).getInWarehouseId();
//                List<WmsSapWarehouseMap> inWarehouseList = wmsSapWarehouseMapService.list(new LambdaQueryWrapper<WmsSapWarehouseMap>().eq(WmsSapWarehouseMap::getWmsBasicWarehouseId, inWarehouseId));
//                if (CollectionUtil.isEmpty(outWarehouseList)) {
//                    throw new JeecgBootException("当前接收库位在wms系统中未找到对应的sap库位");
//                }
//                requestDTO.setAcceptFactory(inWarehouseList.get(0).getSapLocation());
            if (Objects.equals("Y", deliveryTaskMaterial.getLastMasterCode())) {
                requestDTO.setLastMasterCode("X");
            }
            list.add(requestDTO);
        }
        //任务366: 给sap过账时固定调出仓库和调入仓库,(四期到一二期 调出: 1000,调入: 4000,factory使用PT10)
        for (MaterialTransferOrderRequestDTO dto : list) {
            dto.setSetOutFactory("4000");
            dto.setAcceptFactory("1000");
            dto.setFactory("PT10");
        }
        SapBaseResponseDTO sapBaseResponseDTO = sapDockingRepeaterService.sendingOfMaterialTransferOrders(list);
        List<SapBaseResponseDTO> sapBaseResponseDTOS = new ArrayList<>();
        sapBaseResponseDTOS.add(sapBaseResponseDTO);
        log.info("四期到一二期调拨任务结束");
        return sapBaseResponseDTOS;
    }
}
